<?
/**
 * 本文件用于查询数据库中id的文字描述
 *
 * 目前支持的类型有:
 *     UCLibCodes::$nationalities; // 民族
 *     UCLibCodes::$action_types; // 学校/公司类型
 *     UCLibCodes::$genders; // 性别
 *     UCLibCodes::$blood_groups; // 血型
 *     UCLibCodes::$access_types; // 访问类型
 *     UCLibCodes::$special_types;//特殊访问类型
 *     UCLibCodes::$age_parts;//年龄段
 *     UCLibCodes::$twitter_mood;//推他心情
 *     UCLibCodes::$twitter_type;//推他类型
 *     UCLibCodes::$shool_year;//入学年份
 *
 * 使用方式
 * <code>
 *    $name = UCLibCodes::$nationalities->getName(1); // 返回id为1的民族的名称（汉族）
 *    $list = UCLibCodes::$nationalities->getList(); // 返回所有的民族列表
 *    UCLibCodes::$nationalities->toDropdown(); // 打印出一个民族的下拉框
 * </code>
 *
 * @author    徐鹏程 <xupengcheng@snda.com>
 * @version   0.0.1		2010/03/29
 */
class UCLibCodes {

	public static $nationalities; // 民族
	public static $action_types; // 学校/公司类型
	public static $genders; // 性别
	public static $blood_groups; // 血型
	public static $access_types; // 访问类型
	public static $special_types;//特殊访问类型
	public static $age_parts;// 年龄段
	public static $twitter_mood;
	public static $twitter_type;
	public static $shool_year;

	
	/**
	 * 构造函数
	 * @param list: codes的列表
	 */
    function __construct($name, $list) {
		$this->_name = $name;
		$this->_list = $list;
    }

	function _init() {
		if ($this->_list != null) return;
		if ($this == self::$shool_year) {
			$_start = 1970;
			$_end = intval(date("Y", time()));
			$this->_list = array();
			while ($_start <= $_end) {
				$this->_list[$_start] = $_start;
				$_start += 1;
			}
		}
	}

	/**
	 * 根据地点id获取名称
	 * @param id: id
	 * @return 名称
	 */
	public function getName($id) {
		$this->_init();
		return isset($this->_list[$id]) ? $this->_list[$id] : "";
	}

	/**
	 * 检查输入是否合法
	 * @param id: id
	 * @return 1合法，0不合法
	 */
	public function checkValue($id) {
		$this->_init();
		return isset($this->_list[$id]) ? 1 : 0;
	}

	/**
	 * 获取列表
	 */
	public function getList() {
		$this->_init();
		return $this->_list;
	}
	/**
	 * 生成html
	 * @param default_id: 默认选中的id
	 * @param need_empty: 是否需要在前面加上一个“请选择”，默认为添加
	 * @param id: html控件id
	 * @param class: html控件class
	 * @param addtional: 额外的代码，比如"onchange=''" 等等
	 */
	public function toDropdown($default_id = null, $need_empty = true, $id = null, $class=null, $addtional = null) {
		$this->_init();
		echo "<select";
		if ($id) echo " id=\"$id\" name=\"$id\"";
		if ($class) echo " class=\"$class\"";
		if ($addtional) echo " $addtional";
		echo ">";

		if ($need_empty) {
			if ($default_id === null) {
				if ($this->_name)
					echo "<option value=\"\" selected>选择".$this->_name."</option>";
				else
					echo "<option value=\"\" selected>请选择</option>";
			} else {
				if ($this->_name)
					echo "<option value=\"\">选择".$this->_name."</option>";
				else
					echo "<option value=\"\">请选择</option>";
			}
		}

		foreach ($this->_list as $k => $v) {
			if ($k == $default_id) {
				echo "<option value=\"$k\" selected>$v</option>";
			} else {
				echo "<option value=\"$k\">$v</option>";
			}
		}
		echo "</select>";
	}

	private $_list;
	private $_name;
}

UCLibCodes::$nationalities = new UCLibCodes("民族", array (1 => "汉族", 2 => "回族", 3 => "畲族", 4 => "塔塔尔族", 5 => "阿昌族", 6 => "哈萨克族", 7 => "土家族", 8 => "景颇族", 9 => "哈尼族", 10 => "土族", 11 => "白族", 12 => "维吾尔族", 13 => "保安族", 14 => "赫哲族", 15 => "乌孜别克族", 16 => "基诺族", 17 => "布依族", 18 => "拉祜族", 19 => "锡伯族", 20 => "黎族", 21 => "东乡族", 22 => "蒙古族", 23 => "仫佬族", 24 => "达斡尔族", 25 => "藏族", 26 => "毛南族", 27 => "裕固族", 28 => "俄罗斯族", 29 => "德昂族", 30 => "僳僳族", 31 => "瑶族", 32 => "朝鲜族", 33 => "布朗族", 34 => "满族", 35 => "彝族", 36 => "门巴族", 37 => "侗族", 38 => "苗族", 39 => "佤族", 40 => "羌族", 41 => "独龙族", 42 => "怒族", 43 => "珞巴族", 44 => "普米族", 45 => "傣族", 46 => "纳西族", 47 => "高山族", 48 => "壮族", 49 => "额伦春族", 50 => "塔吉克族", 51 => "京族", 52 => "仡佬族", 53 => "鄂温克族", 54 => "撒拉族", 55 => "柯尔克孜族", 56 => "水族", 99 => "其他" ));

UCLibCodes::$action_types = new UCLibCodes("类型",array (1 => "大学", 2 => "高中/职高", 3 => "初中", 4 => "小学", 5 => "公司/机构"));
UCLibCodes::$genders = new UCLibCodes("性别",array (1 => "男", 2 => "女"));
UCLibCodes::$blood_groups = new UCLibCodes("血型",array (1 => "A型", 2 => "B型", 3 => "AB型", 4 => "O型"));
UCLibCodes::$access_types = new UCLibCodes("",array (1 => "所有人可见", 2 => "好友可见", 3 => "只有我可见"));
UCLibCodes::$special_types = new UCLibCodes("",array (1 => "所有人可见", 2 => "好友可见", 3 => "只有我可见",4 => "单独设置每一项联系方式"));
UCLibCodes::$age_parts = new UCLibCodes("",array (1 => "不限", 2 => "0-18岁", 3 => "18-25岁",4 => "25-30岁",5 => "30-40岁",6 => "40岁以上"));
UCLibCodes::$twitter_mood = new UCLibCodes("推他心情",array ( 0=>"一般",1 => "狂喜", 2 => "期待", 3 => "八卦",4 => "伤心",5=>"郁闷",6 => "囧rz", 7 => "分享", 8 => "碎碎念",9 => "求"));
UCLibCodes::$twitter_type = new UCLibCodes("推他类型",array ( 0=>"全部",1 => "文字", 2 => "图片", 3 => "视频"));
UCLibCodes::$shool_year = new UCLibCodes("入学年份", null);
?>